package com.windoent.yugioh.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.windoent.yugioh.R;
import com.windoent.yugioh.utils.BaseMethod;

/**
 * 作者：Android
 * 时间：2019/1/16 11:08
 * 功能：繼承Dialog，自定义更新Dialog
 */
public class CanSaiBaoMingApplyCaiPanDialog extends Dialog {

    private TextView mTvTitle;
    private TextView mTvCancel;
    private TextView mTvSure;
    private TextView mTvTime;
    private TextView mTvPlace;
    private TextView mTvPhone;


    private String titleStr;//从外界设置的title文本
    private String placeStr;//从外界设置的消息文本
    private String timeStr;
    private String phoneStr;
    //确定文本和取消文本的显示内容
    private String yesStr, noStr;

    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器

    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(onNoOnclickListener onNoOnclickListener) {
        this.noOnclickListener = onNoOnclickListener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(onYesOnclickListener onYesOnclickListener) {
        this.yesOnclickListener = onYesOnclickListener;
    }

    public CanSaiBaoMingApplyCaiPanDialog(Context context) {
        super(context, R.style.MoHUDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.free_cansai_baoming_apply_caipan_yn_dialog_layout);
        //按空白处能取消动画
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.CENTER;
        attributes.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(attributes);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        mTvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yesOnclickListener != null) {
                    yesOnclickListener.onYesClick();
                    dismiss();
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        //如果用户自定了title和message
        if (BaseMethod.notNull(titleStr)) {
            mTvTitle.setText(titleStr);
        }
        if (BaseMethod.notNull(timeStr)) {
            mTvTime.setText(timeStr);
        }
        if (BaseMethod.notNull(placeStr)) {
            mTvPlace.setText(placeStr);
        }
        if (BaseMethod.notNull(phoneStr)) {
            mTvPhone.setText(phoneStr);
        }
        if (BaseMethod.notNull(yesStr)) {
            mTvSure.setText(yesStr);
        }
        if (BaseMethod.notNull(noStr)) {
            mTvCancel.setText(noStr);
        }

    }

    /**
     * 初始化界面控件
     */
    private void initView() {

        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvCancel = (TextView) findViewById(R.id.tv_cancel);
        mTvSure = (TextView) findViewById(R.id.tv_sure);
        mTvTime = (TextView) findViewById(R.id.tv_time);
        mTvPlace = (TextView) findViewById(R.id.tv_place);
        mTvPhone = (TextView) findViewById(R.id.tv_phone);

    }

    /**
     * 从外界Activity为Dialog设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleStr = title;
    }

    /**
     * 从外界Activity为Dialog设置dialog的message
     */
    public void setPlaceStr(String str) {
        this.placeStr = str;
    }

    public void setTimeStr(String str) {
        this.timeStr = str;
    }

    public void setPhoneStr(String str) {
        this.phoneStr = str;
    }

    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onYesOnclickListener {
        public void onYesClick();
    }

    public interface onNoOnclickListener {
        public void onNoClick();
    }
}
